package com.bwie.controller;

import com.bwie.bean.Receiving;
import com.bwie.bean.User;
import com.bwie.bean.Zmark;
import com.bwie.service.ReceivingService;
import com.bwie.util.FileUploadUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class ReceivingController {

    @Autowired
    ReceivingService receivingService;

    @RequestMapping("list.do")
    public String list(
            @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
            Model model,
            String rodd,
            String zdateX,
            String zdateD,
            String rdateX,
            String rdateD,
            String priortyX,
            String priortyD,
            String kname,
            String clist,
            String zname,
            String zjob,
            String remark,
            String elems//优先级

    ){
        Map map = new HashMap();
        map.put("rodd",rodd);
        map.put("zdateX",zdateX);
        map.put("zdateD",zdateD);
        map.put("rdateX",rdateX);
        map.put("rdateD",rdateD);
        map.put("priortyX",priortyX);
        map.put("priortyD",priortyD);
        map.put("kname",kname);
        map.put("clist",clist);
        map.put("zname",zname);
        map.put("zjob",zjob);
        map.put("remark",remark);
        map.put("elems",elems);
        PageHelper.startPage(pageNum,2);
        List<Receiving>list=receivingService.list(map);
        PageInfo pageInfo = new PageInfo(list);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("map",map);
        return "list";
    }
    //动态获取所有的制单人
    @RequestMapping("listZmark.do")
    @ResponseBody
    public List<Zmark>listZmark(){
        List<Zmark>listZmark=receivingService.listZmark();
        return listZmark;
    }

    //添加
    @RequestMapping("toaddgly.do")
    public String toaddgly(){
        return "addGly";
    }
    @RequestMapping("addGly.do")
    @ResponseBody
    public Object addGly(Receiving receiving, @RequestParam("photo")MultipartFile photo, HttpSession session){
        if (photo!=null){
            String upload = FileUploadUtil.load(session, photo, "upload");
            receiving.setPic(upload);
        }
        int a=receivingService.addGly(receiving);
        return a;
    }
    //修改
    @RequestMapping("updateByRid.do")
    @ResponseBody
    public Object updateByRid(Integer rid){
        int a=receivingService.updateByRid(rid);
        return a;
    }

    //登录
    @RequestMapping("login.do")
    public String login(User user,HttpSession session){
        User user1=receivingService.listUser(user);
        if (user1!=null){
            if (user1.getSid()==0){
                session.setAttribute("user",user);
                return "redirect:listStu.do";
            }else {
                return "redirect:list.do";
            }
        }else {
            return "redirect:index.jsp";
        }
    }

    @RequestMapping("listStu.do")
    public String listStu(Model model,HttpSession session){
        User user = (User) session.getAttribute("user");
        List<Receiving>listStu=receivingService.listStu(user.getUname());
        model.addAttribute("listStu",listStu);
        return "listStu";
    }
}
